//###############
//FILE : NameWantAllFilesSession.java
//WRITER : Daniel Lowengrub, daniel_i_l, 320687098
//EXERCISE : oop ex3 2011
//DESCRIPTION:
//See below
//###############

package oop.ex3.protocol.sessions;

import oop.ex3.protocol.messages.NsContainFileMsg;
import oop.ex3.protocol.messages.WantAllFilesMsg;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import oop.ex3.exceptions.InvalidMessageException;

/**
 * Send a FM a list of the files it knows about.
 * @author daniel
 */
public class NameWantAllFilesSession extends NameToFileSession {
    private ArrayList<String> _fileNames;

    /**
	 * Create a new session.
	 */
    public NameWantAllFilesSession() {
        super(new WantAllFilesMsg());
    }

    /**
     * Set the list of files to send.
     * @param fileNames The list of files.
     */
    public void setFileNames(ArrayList<String> fileNames) {
        _fileNames = fileNames;
    }

    @Override
    protected boolean specExecuteSession(DataInputStream inputStream, DataOutputStream outputStream) throws IOException, InvalidMessageException {
        WantAllFilesMsg wantFiles = new WantAllFilesMsg();
        wantFiles.receive(inputStream);

        //if we received the request then send the list
        
        //if (_fileNames.size() == 0) {
        //    NoFileExistMsg noFile = new NoFileExistMsg();
        //    noFile.send(outputStream);
        //    return true;
        //}


        for (String fileName : _fileNames) {
            NsContainFileMsg containFile = new NsContainFileMsg();
            containFile.setFileName(fileName);
            containFile.send(outputStream);
        }

        _endList.send(outputStream);

        return true;
    }

}
